<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">ID</label>
        <div class="layui-input-block">
            <input type="text" name="id" lay-verify="required" lay-reqtext="ID不能为空" placeholder="" value="" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">接头ID</label>
        <div class="layui-input-block">
            <input type="text" name="bindJointid" lay-verify="required" lay-reqtext="接头ID不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">球机ID</label>
        <div class="layui-input-block">
            <input type="text" name="cameraId" lay-verify="required" lay-reqtext="球机ID不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">球机名称</label>
        <div class="layui-input-block">
            <input type="text" name="cameraName" lay-verify="required" lay-reqtext="球机名称不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">IP</label>
        <div class="layui-input-block">
            <input type="text" name="ip" lay-verify="required" lay-reqtext="IP不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">端口</label>
        <div class="layui-input-block">
            <input type="text" name="port" lay-verify="required" lay-reqtext="端口不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">是否自动</label>
        <div class="layui-input-block">
            <input type="radio" name="enable" value="0" title="自动" id="autoRadio">
            <input type="radio" name="enable" value="1" title="手动" id="manualRadio">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <label class="layui-form-label required"></label>
        <div class="layui-input-block">
            <input type="text" name="createTime" lay-verify="required" lay-reqtext="模式不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <label class="layui-form-label required"></label>
        <div class="layui-input-block">
            <input type="text" name="updateTime" lay-verify="required" lay-reqtext="模式不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <label class="layui-form-label required"></label>
        <div class="layui-input-block">
            <input type="text" name="isDelete" lay-verify="required" lay-reqtext="模式不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    // 获取URL中的查询字符串
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);
    // 通过参数名获取传递的设备信息
    const id = urlParams.get('id');

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        $(document).ready(function () {
            // 后台获取所有菜单数据的逻辑，可以使用 AJAX 请求
            $.ajax({
                url: '/camera/getById', // 替换为实际的后台接口 URL
                method: 'GET',
                data: {
                    id:id
                },
                dataType: 'json',
                success: function (response) {
                    if (response.code === 0) {
                        var data = response.data;
                        const id = document.querySelector(`input[name="id"]`);
                        const bindJointid = document.querySelector(`input[name="bindJointid"]`);
                        const cameraId = document.querySelector(`input[name="cameraId"]`);
                        const cameraName = document.querySelector(`input[name="cameraName"]`);
                        const ip = document.querySelector(`input[name="ip"]`);
                        const port = document.querySelector(`input[name="port"]`);
                        const username = document.querySelector(`input[name="username"]`);
                        const password = document.querySelector(`input[name="password"]`);
                        const createTime = document.querySelector(`input[name="createTime"]`);
                        const updateTime = document.querySelector(`input[name="updateTime"]`);
                        const isDelete = document.querySelector(`input[name="isDelete"]`);
                        id.value = data.id;
                        bindJointid.value = data.bindJointid;
                        cameraId.value = data.cameraId;
                        cameraName.value = data.cameraName;
                        ip.value = data.ip;
                        port.value = data.port;
                        username.value = data.username;
                        password.value = data.password;
                        createTime.value = data.createTime;
                        updateTime.value = data.updateTime;
                        isDelete.value = data.isDelete;
                        var autoRadio = document.querySelector('input[value="0"]');
                        var manualRadio = document.querySelector('input[value="1"]');
                        if (data.enable === 0) {
                            autoRadio.checked = true;
                        } else {
                            manualRadio.checked = true;
                        }
                        layui.form.render();
                    } else {
                        console.error('Failed to fetch camera data: ' + response.msg);
                    }
                },
                error: function (xhr, status, error) {
                    console.error('AJAX request failed: ' + error);
                }
            });
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url:  "/camera/edit",    //请求的url地址
                dataType: "json",   //返回格式为json
                async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                data: data.field,    //参数值,键值对
                type: "POST",   //请求方式
                success: function (result) {
                    if (result.code == 0) {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layui.table.reload('currentTableId');
                        parent.layer.close(iframeIndex);
                    }else {
                        layer.msg(result.msg);
                    }
                },
                error: function () {
                    return false;
                }
            });
        });

    });
</script>
</body>
</html>